﻿"use strict";
define(['admin/app', "service/admin/transferService", "service/admin/merchantService"], function (app) {
    app.controller('transferController', function ($scope, $stateParams, $location, $uibModal, $state, utils, transferService, merchantService) {
        $scope.query = {
            page: 1, size: 20, pay_channel: 1, name: '', mobile: '', start_time: '', end_time: '', merchant_no: '', order_no: '', status: 0, merchant_id: 0
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                transferService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            search_page:function(page){
            	$scope.query.page = page;
	            $location.search('page', $scope.query.page);
            },
            get_oems: function () {
                merchantService.getoem().success(function(response) {
                    $scope.oems = response.data;
                    $scope.oems.unshift({id:0,name:"全部"});
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    transferService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            goBack: function () {
                history.back();
            },
            get_status_name:function(status){
            	if(status==1)return '提现申请';
            	else if(status==2)return '完成';
            	else if(status==3)return '转账中';
            	else if(status==4)return '失败';
            	else if(status==5)return '不确定';
            	else if(status==6)return '取消';
            },
            apply_batch: function () {
                $scope.psdisabled = true;
            	transferService.apply_batch($scope.query).success(function (response) {
                   utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) 
                        $location.url('app/finance/transfer_batch');
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            get_balance: function () {
                $scope.psdisabled = true;
            	transferService.get_balance({pay_channel:$scope.query.pay_channel}).success(function (response) {
                   utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        $scope.model.channel_total=response.total;
                        $scope.model.balance=response.balance;
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            batch_search: function () {
                transferService.getBatchs($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get_batch_status_name:function(status){
            	if(status==1)return '报盘文件';
            	else if(status==2)return '报盘通知';
            	else if(status==3)return '已回盘';
            	else if(status==4)return '已处理';
            },
            batch_notify: function (item) {
                $scope.psdisabled = true;
                transferService.batch_notify(item).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.batch_search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            batch_back: function (item) {
                $scope.psdisabled = true;
                transferService.batch_back(item).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.batch_search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            payoff:function(item){
            	$scope.psdisabled = true;
                transferService.payoff({id:item.id,pay_channel:item.pay_info.pay_channel}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            transfer_first:function(item){
            	$scope.psdisabled = true;
                transferService.transfer_first({merchant_no:item.merchant.merchant_no,order_no:item.order_no,pay_channel:item.pay_info.pay_channel}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            cancel:function(item){
            	$scope.psdisabled = true;
                transferService.cancel({id:item.id}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.finance.transfer_detail')) {
            methods.get();
        } 
        if ($state.includes('app.finance.transfer_batch')) {
            methods.batch_search();
        }else {
        	methods.search();
        	methods.get_oems();
    	}
    });
});
